package com.example.myspringboot.service.shujuchuli;

import com.example.myspringboot.anno.Loggable;
import lombok.Cleanup;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * @author cy
 * @date 2024/5/29
 */
@Service
public class HandleDataServiceImpl {

    /**
     * 上传文件的方法
     *
     * @param fileName 文件名，用于标识上传的文件
     * @param file     上传的文件对象
     * @return 返回上传结果的字符串表示
     */
    @Loggable
    public String uploadFile(String fileName, MultipartFile file) {
        try {
            @Cleanup InputStream inputStream = file.getInputStream();
            File file1 = new File("doc" + File.separator + fileName);
            @Cleanup FileOutputStream fileOutputStream = new FileOutputStream(file1);
            int len;
            byte[] data = new byte[1024 * 10];
            while ((len = inputStream.read(data)) != -1) {
                fileOutputStream.write(data, 0, len);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
